package my.edu.model.information.controller;

import cn.hutool.json.JSONObject;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.tags.Tag;
import jakarta.validation.Valid;
import jakarta.validation.constraints.NotBlank;
import my.edu.common.domain.PageDTO;
import my.edu.common.validate.groups.Insert;
import my.edu.common.domain.Response;
import my.edu.model.information.entity.dto.SupplierDTO;
import my.edu.model.information.entity.dto.SupplierSearchDTO;
import my.edu.model.information.service.ISupplierService;
import my.edu.utils.StringUtils;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RequestMapping("/supplier")
@RestController
@Tag(name = "供应商接口")
@Validated
public class SupplierController {
    @Autowired
   private ISupplierService supplierService;

    @PutMapping
    @RequiresPermissions("supplier:save")
    @Operation(summary = "新增供应商")
    public Response save(@Validated(value = Insert.class) @RequestBody SupplierDTO supplierDTO) {
        supplierService.savenew(supplierDTO);
        return Response.success();
    }


    @GetMapping("/list")
    @RequiresPermissions("supplier:get")
    @Operation(summary = "获取供应商列表")
    public Response getCustomer(@ModelAttribute PageDTO pageDTO, SupplierSearchDTO supplierSearchDTO) {
        return Response.success(supplierService.listAll(pageDTO, supplierSearchDTO));
    }

    @GetMapping
    @Operation(summary = "获取供应商名称列表")
    public Response listAllOnly() {
        return Response.success(supplierService.listOnly());
    }



    @GetMapping("/one")
    @RequiresPermissions("supplier:get")
    @Operation(summary = "根据编号获取供应商信息")
    public Response getCustomerByOn(@NotBlank(message = "id不能为空") String id) {
         return Response.success(supplierService.getVOById(id));
    }


    @PostMapping("/update")
    @RequiresPermissions("supplier:update")
    @Operation(summary = "更新供应商信息")
    public Response update(@Valid @RequestBody SupplierDTO supplierDTO) {
        supplierService.updateByDTO(supplierDTO);
        return Response.success();
    }

    @PostMapping("/batchSetStatus")
    @RequiresPermissions(value = {"system:user"})
    @Operation(summary = "更新供应商状态")
    public Response batchSetStatus(@RequestBody JSONObject jsonObject){
        int status = jsonObject.getInt("status");
        String ids = jsonObject.getStr("ids");
        if(status!=0&&status!=1){
            return Response.error("状态值错误");
        }
        List<Long> longs = StringUtils.strToLongList(ids);
        supplierService.setStatus(longs,status);
        return Response.success();
    }

}
